package com.hmall.gateway.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
@Slf4j
public class CorsFilter implements GlobalFilter , Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //跨域过滤
        ServerWebExchange webExchange = exchange.mutate()
                .request(builder ->
                        builder.header("Access-Control-Allow-Origin", "*")
                                .header("Access-Control-Allow-Methods","GET,PUT,POST,DELETE,OPTIONS")
                                .header("Access-Control-Allow-Headers","Content-Type")).build();
        //放行
        //调用下一个过滤器的filter，执行下一个过滤器
        return chain.filter(webExchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
